package com.lottery.ruok.lottrey.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.applozic.mobicomkit.uiwidgets.conversation.ConversationUIService;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.dialog.PasswordDialog;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRongUserInfoRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetProfileResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRongUserInfoReponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

import static com.lottery.ruok.lottrey.activity.SendRedPacketActivity.EXTRA_TARGET_ID;

/*
* 发红包
* */

public class SingleSendRedPacketActivity extends BaseActivity implements TextWatcher{

    @BindView(R.id.single_packet_money)
    EditText singlePacketMoney;
    @BindView(R.id.blessing)
    EditText blessing;
    @BindView(R.id.total_money)
    TextView totalMoney;
    @BindView(R.id.confirm)
    TextView confirm;

    private int pocketType = 1;//2是群发红包_普通，3是群发红包_拼手气，1是单独发红包
    private String toUserName;
    private String targetId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_activity_send_red);
        ButterKnife.bind(this);
     targetId=   getIntent().getStringExtra(EXTRA_TARGET_ID);
        getUserName();
        init();
    }

    private void init() {
        /**
         * 小数点后保留两位
         */

        singlePacketMoney.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                s = s.toString().subSequence(0,
                        s.toString().indexOf(".") + 3);
                singlePacketMoney.setText(s);
                singlePacketMoney.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            singlePacketMoney.setText(s);
            singlePacketMoney.setSelection(2);
        }

        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                singlePacketMoney.setText(s.subSequence(0, 1));
                singlePacketMoney.setSelection(1);
                return;
            }
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

       listenToInput();
    }

    private void listenToInput(){
        if(singlePacketMoney.getText().toString().equals("")
                || singlePacketMoney.getText().toString().equals(".")
                || Double.parseDouble(singlePacketMoney.getText().toString()) == 0f){
            confirm.setTextColor(Color.parseColor("#7E33CD"));
            confirm.setEnabled(false);

            totalMoney.setText(0 + "");
        } else {
            confirm.setTextColor(Color.parseColor("#ffffff"));
            confirm.setEnabled(true);

            totalMoney.setText(Utils.saveTwoFormat(Double.parseDouble(singlePacketMoney.getText().toString())));
        }
    }

    @OnClick({R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                updateAccount(new AfterUpdateAccount() {
                    @Override
                    public void afterUpdateAccount(GetProfileResponse getProfileResponse) {
                        if (getProfileResponse.getUser().getWithdraw_password() == 1) {
                            PasswordDialog passwordDialog = new PasswordDialog();
                            Bundle bundle = new Bundle();
                            /*bundle.putInt("type", 1);
                            bundle.putInt("pocketType", pocketType);
                            bundle.putDouble("amount", Double.parseDouble(totalMoney.getText().toString()));
                            bundle.putDouble("per_amount", Double.parseDouble(singlePacketMoney.getText().toString()));
                            bundle.putInt("numbers", 1);
                            bundle.putString("desc", blessing.getText().toString());
                            bundle.putString("to_user_id", getIntent().getStringExtra(ConversationUIService.USER_ID));
                            bundle.putInt("room_id", 0);*/

                            bundle.putInt(PasswordDialog.TYPE, 1);
                            bundle.putInt(PasswordDialog.POCKET_TYPE, pocketType);
                            bundle.putDouble(PasswordDialog.AMOUNT, Double.parseDouble(totalMoney.getText().toString()));
                            bundle.putDouble(PasswordDialog.PER_AMOUNT, Double.parseDouble(singlePacketMoney.getText().toString()));
                            bundle.putInt(PasswordDialog.NUMBERS, 1);
                            bundle.putString(PasswordDialog.DESC, blessing.getText().toString());
                            bundle.putString(PasswordDialog.TARGET_ID, targetId);
                            bundle.putString(PasswordDialog.TO_USER_NAME, toUserName);

                            passwordDialog.setArguments(bundle);
                            passwordDialog.show(getSupportFragmentManager(), null);
                        } else {
                            Utils.showToast("您没有设置资金密码，请前往设置");
                        }
                    }
                }, true);
                break;
        }
    }
    private void getUserName() {
        RetrofitBase.getRetrofit().create(GetRongUserInfoRequest.class).getResult(null,null, targetId)
                .compose(RxHelper.<GetRongUserInfoReponse>handleResult())
                .subscribe(new RxSubscribe<GetRongUserInfoReponse>(this) {
                    @Override
                    protected void _onNext(GetRongUserInfoReponse getRongUserInfoReponse) {
                        if (getRongUserInfoReponse!=null){
                            toUserName=          getRongUserInfoReponse.getList().get(0).getUsername();
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }
}
